package Repositorio;

import View.Novo;
import View.Solucao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.table.DefaultTableModel;

public class RepositorioDefeito {
    
    private Connection conn;
    private String query;
    private PreparedStatement stmt;
    private ResultSet rs;
    
    public void verifySolucao(String defeito) throws SQLException, ClassNotFoundException {
        conn = LinkDB.Conexao.getConexao();
        query = "SELECT os.solucao, os.defeito, equipamento.nameEquipamento FROM os, equipamento "
                + "WHERE os.defeito LIKE CONCAT('%', ?, '%') AND "
                + "(equipamento.idEquipamento = os.idEquipamento) AND (equipamento.nameEquipamento = ?) "
                + "GROUP BY os.solucao ORDER BY os.idOs ASC";        
        stmt = conn.prepareStatement(query);
        stmt.setString(1, defeito);
        stmt.setString(2, Novo.equipamento.getSelectedItem().toString());
        rs = stmt.executeQuery();        
        DefaultTableModel model = (DefaultTableModel) Solucao.tabelaSolucao.getModel();
        while (rs.next()) {
            model.addRow(new Object[]{
                        rs.getString("equipamento.nameEquipamento"),
                        rs.getString("os.defeito"),
                        rs.getString("os.solucao")
                    });            
        }
        rs.close();
        stmt.close();
        conn.close();
    }
    
}
